<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@include file="/web/page/head.jsp" %>
<title>Insert title here</title>
</head>
<body>
<form class="layui-form" method="post">
			
			<br></br>
			<div class="layui-form-item">
			<label class="layui-form-label">编号:</label>
			<div class="layui-input-inline">
				<input type="text" name="code" class="layui-input" lay-verity="required" placeholder="必填" />
			</div>
			
			<label class="layui-form-label">名称:</label>
			<div class="layui-input-inline">
				<input type="text" name="name" class="layui-input" lay-verity="required" placeholder="必填" />
			</div>
			</div>
			
<!-- 			<div class="layui-form-item"> -->
<!-- 			<label class="layui-form-label">名称:</label> -->
<!-- 			<div class="layui-input-inline"> -->
<!-- 				<input type="text" name="name" class="layui-input" lay-verity="required" placeholder="必填" /> -->
<!-- 			</div> -->
<!-- 			</div> -->
			
			<div class="layui-form-item">
			<label class="layui-form-label">价格:</label>
			<div class="layui-input-inline">
				<input type="text" name="price" class="layui-input" lay-verity="required" placeholder="必填" />
			</div>
			
			<label class="layui-form-label">库存:</label>
			<div class="layui-input-inline">
				<input type="text" name="num" class="layui-input" lay-verity="required" placeholder="必填" />
			</div>
			</div>
			
<!-- 			<div class="layui-form-item"> -->
<!-- 			<label class="layui-form-label">库存:</label> -->
<!-- 			<div class="layui-input-inline"> -->
<!-- 				<input type="text" name="num" class="layui-input" lay-verity="required" placeholder="必填" /> -->
<!-- 			</div> -->
<!-- 			</div> -->
			
<!-- 			<div class="layui-form-item"> -->
<!-- 			<label class="layui-form-label">状态:</label> -->
<!-- 			<div class="layui-input-inline"> -->
<!-- 				<input type="text" name="state" class="layui-input" lay-verity="required" placeholder="必填" /> -->
<!-- 			</div> -->
<!-- 			</div> -->
			
			<div class="layui-form-item">
            <label class="layui-form-label">状态:</label>
            <div class="layui-input-inline">
                <select name="state" lay-verify="required" lay-search="">
                	<option value="">请选择状态</option>
                	<option value="上架">上架</option>
          			<option value="已下架">已下架</option>
          			<option value="已售罄">已售罄</option>
                </select>
            </div>
            
            <label class="layui-form-label">描述:</label>
			<div class="layui-input-inline">
				<input type="text" name="descr" class="layui-input" lay-verity="required" placeholder="必填" />
			</div>
			</div>
<!--         	</div> -->
			
<!-- 			<div class="layui-form-item"> -->
<!-- 			<label class="layui-form-label">描述:</label> -->
<!-- 			<div class="layui-input-inline"> -->
<!-- 				<input type="text" name="descr" class="layui-input" lay-verity="required" placeholder="必填" /> -->
<!-- 			</div> -->
<!-- 			</div> -->
			
<!-- 			<div class="layui-form-item"> -->
<!-- 			<label class="layui-form-label">上次:</label> -->
<!-- 			<div class="layui-input-inline"> -->
<!-- 				<input type="text" name="parent" class="layui-input" lay-verity="required" placeholder="必填" /> -->
<!-- 			</div> -->
<!-- 			</div> -->
			
			<div class="layui-form-item">
            <label class="layui-form-label">一级分类:</label>
            <div class="layui-input-inline">
                <select name="parentA" lay-verify="required" lay-filter="parentA" lay-search="">
<!--                 	<option value="aaa">请选择</option> -->
<!--                 	<option value="上架">a1</option> -->
<!--           			<option value="已下架">b1</option> -->
<!--           			<option value="已售罄">c1</option> -->
                </select>
            </div>
            
            
            <label class="layui-form-label">二级分类:</label>
            <div class="layui-input-inline">
                <select name="parent" lay-verify="required" lay-filter="parent" lay-search="">
<!--                 		<option value="bbb">请选择</option> -->
                </select>
            </div>
			</div>
			
			<div class="layui-form-item">
			<label class="layui-form-label"></label>
			<div class="layui-input-inline">
				<input type="button" class="layui-btn" lay-submit lay-filter="add" value="确定" />
				<input type="button" class="layui-btn" onclick="closeThis()" value="取消" />
			</div>
			</div>
			<input type="hidden" name="action" value="add" />
</form>

<script type="text/javascript">
var layer = layui.layer;
var $ = layui.jquery;
var laytpl = layui.laytpl;
var element = layui.element;
var form = layui.form
var code = '<%=request.getParameter("code")%>';
element.render();
form.render();

// getSelect('parentA','aaa')
initA();
function initA(){
// 	if(parentand==''){
// 		$("select[name^='parent']").empty()
// 		form.render('select');
// 		return;
// 	}
	$.ajax({
		url:"<%=app%>/goodsA/test.do",
// 		data:{code:code},
		data:{action:"test"},
		dataType:'json',
		type:'post',
		success:function(data){
			console.log(data)
			var html="<option value=''>请选择</option>";
			$.each(data,function(i,dom){
				html+="<option value='"+dom.code+"'>"+dom.name+"</option>";
			});
			$("select[name='parentA']").html(html);
			form.render();
		}
	})
}
form.on('select(parentA)',function(data){
	console.log(data.value);
	var parenttwo = data.value;
	console.log(parenttwo)
	$.ajax({
		url:"<%=app%>/goodsA/get.do",
		data:{parentCode:parenttwo},
		dataType:"json",
		type:"post",
		success:function(data){
			console.log(data)
			var html="<option value=''>请选择</option>";
			$.each(data,function(i,dom){
				html+="<option value='"+dom.name+"'>"+dom.name+"</option>";
			});
			$("select[name='parent']").html(html);
			form.render('select');
		}
	});
})
// form.on('select(parent)',function(data){
// 	getSelect('parent'.data.value);
// });

function closeThis(){
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	parent.layer.close(index); //再执行关闭   
}
form.on("submit(add)",function(data){
// 	console.log(data)
	$.ajax({
		url:"<%=app%>/goodsB/add.do",
		data:data.field,
		dataType:'text',
		type:'post',
		success:function(data){
			console.log(data)
			if(data=="1"){
				layer.msg('添加成功',closeThis);
			}else if(data=="rgp"){
				layer.msg('账号重复');
			}else{
				layer.msg('添加失败');
			}
		}
	})
})
</script>
</body>
</html>